<template>
  <div class="role-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 使用 Form 组件：行内表单 -->
        <el-form :inline="true" :model="form" class="demo-form-inline" ref="form" size="small">
          <span>筛选搜索</span>
          <div>
            <el-button
              type="primary"
              size="small"
              @click="onSubmit"
              :disabled="isLoading"
              >查询搜索</el-button
            >
            <el-button size="small" @click="onReset">重置</el-button>
          </div>
          <div style="margin-top: 15px">
            <el-form-item label="输入搜索：" prop="name">
              <el-input
                v-model="form.name"
                placeholder="角色名称"
                clearable
              ></el-input>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <el-button size="small"  @click="handleAdd">添加角色</el-button>
      <el-table
        :data="allRoleInfo"
        v-loading="isLoading"
        border
        style="width: 100%"
      >
        <el-table-column
          fixed
          align="center"
          type="index"
          label="编号"
          width="100"
        >
        </el-table-column>
        <el-table-column align="center" prop="name" label="角色名称">
        </el-table-column>
        <el-table-column prop="description" align="center" label="描述">
        </el-table-column>
        <!-- 设置过滤器需要使用作用域插槽获取数据 -->
        <el-table-column align="center" label="添加时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdTime | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="250">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="$router.push({
                name: 'alloc-menu',
                params: {
                  roleId: scope.row.id
                }
              })"
            >分配菜单</el-button>
            <el-button
              size="mini"
              @click="$router.push({
                name: 'alloc-resource',
                params: {
                  roleId:scope.row.id
                }
              })"
              type="text"
              >分配资源</el-button>
            <br>
            <el-button size="mini" @click="handleEdit(scope.row)" type="text"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加角色的对话框结构 -->
      <el-dialog
        :title="isEdit ? '编辑角色' : '添加角色'"
        :visible.sync="dialogVisible"
        width="40%">
        <!-- 将添加与编辑功能单独封装到组件 -->
        <create-or-edit
          v-if="dialogVisible"
          :isEdit="isEdit"
          :role-id="roleId"
          @success="handleSuccess"
          @cancel="dialogVisible=false"></create-or-edit>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getRole, deleteRole } from '@/services/role'
import createOrEdit from './create-or-edit.vue'

export default {
  components: { createOrEdit },
  name: 'RoleList',
  data () {
    return {
      form: {
        name: '',
        description: ''
      },
      allRoleInfo: [],
      // 用于保存加载状态
      isLoading: false,
      // 控制对话框显示
      dialogVisible: false,
      // 是编辑状态还是添加状态
      isEdit: true,
      // 存储正在编辑的角色 ID
      roleId: ''
    }
  },
  created () {
    // 获取所有角色
    this.loadRoleInfo()
  },

  methods: {
    // 监听子组件的添加状态，成功时触发
    handleSuccess () {
      // 隐藏对话框
      this.dialogVisible = false
      // 刷新列表
      this.loadRoleInfo()
    },
    // 重置按钮点击操作
    onReset () {
      this.$refs.form.resetFields()
    },
    // 提交筛选
    onSubmit () {
      this.loadRoleInfo()
    },
    // 添加角色
    handleAdd () {
      this.dialogVisible = true
      this.isEdit = false
    },
    // 编辑角色
    handleEdit (rowData) {
      this.dialogVisible = true
      this.isEdit = true
      this.roleId = rowData.id
    },
    handleDelete (rowData) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await deleteRole(rowData.id)
        if (data.code === '000000') {
          this.$message('删除成功!')
          // 更新数据列表
          this.loadRoleInfo()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 获取角色信息
    async loadRoleInfo () {
      this.isLoading = true
      const { data } = await getRole(this.form)
      if (data.code === '000000') {
        this.allRoleInfo = data.data.records
        this.isLoading = false
      }
    }
  },
  filters: {
    // 日期过滤器
    dateFormat (date) {
      date = new Date(date)
      return `
        ${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
        ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}
      `
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card .el-form {
    .el-button {
      float: right;
      margin-left: 15px;
    }

    div .el-form-item {
      padding-left: 50px;
    }
  }

  .el-table {
    margin-top: 20px;
  }
  </style>
